package edu.stu.book.book.keeping.ui.screen.home

import android.content.Context
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import edu.stu.book.book.keeping.R
import edu.stu.book.book.keeping.component.CostHeaderItem
import edu.stu.book.book.keeping.component.CostItem
import edu.stu.book.book.keeping.component.IconText
import edu.stu.book.book.keeping.data.NavigationItem
import edu.stu.book.book.keeping.data.indexMenuList
import edu.stu.book.book.keeping.db.dao.Cost
import edu.stu.book.book.keeping.extensions.popTip
import edu.stu.book.book.keeping.extensions.rememberOverscrollFlingBehavior
import edu.stu.book.book.keeping.utils.ColorUtils
import edu.stu.book.book.keeping.utils.TimeUtils
import edu.stu.book.book.keeping.viewmodel.IndexViewModel

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun IndexScreen(
    navController: NavHostController,
    viewModel: IndexViewModel = IndexViewModel(),
    modifier: Modifier = Modifier
) {

    val context = LocalContext.current

    Column(
        modifier = modifier
            .fillMaxSize()
            .background(
                brush = Brush.verticalGradient(
                    colors = listOf(
                        ColorUtils.getColor(context, R.color.main_color_20),
                        Color.White,
                        Color.White
                    )
                )
            )
    ) {
        // 顶部的每日一句
        TopTipText(context)

        Spacer(modifier.padding(top = 10.dp))

        // 总计
        TopAppBar()

        Spacer(modifier.padding(top = 10.dp))

        //
        MenuNav(context, indexMenuList)

        ContentItem(
            context,
            viewModel.costList[0].year,
            viewModel.costList[0].month,
            viewModel.getCostList()
        )
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun ContentItem(
    context: Context,
    year: Int,
    month: Int,
    costList: Map<Int, Triple<Double, Double, List<Cost>>>,
    modifier: Modifier = Modifier
) {

    val rememberedCostList = remember { costList }

    val lazyListState = rememberLazyListState()

    val tint = ColorFilter.tint(ColorUtils.getColor(context))

    LazyColumn(
        state = lazyListState,
        modifier = modifier
            .fillMaxSize()
            .background(Color.White, RoundedCornerShape(8.dp))
            .padding(top = 10.dp, bottom = 60.dp, start = 10.dp, end = 10.dp),
        flingBehavior = rememberOverscrollFlingBehavior { lazyListState }


    ) {
        rememberedCostList.forEach { (day, costTriple) ->
            stickyHeader(key = day) {
                CostHeaderItem(
                    context = context,
                    "${month}月${day}日 ${TimeUtils.getDayOfWeek(year, month, day)}",
                    costTriple.first,
                    costTriple.second,
                    modifier
                )
            }
            items(costTriple.third, key = { it.id }) { cost ->
                CostItem(
                    context = context,
                    cost = cost,
                    modifier
                ) {
                    "单机了花费".popTip()
                }
                Spacer(modifier.padding(top = 5.dp))
            }
        }
    }
}


@Composable
fun MenuNav(context: Context, dataList: List<NavigationItem>, modifier: Modifier = Modifier) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(4.dp)
            .background(Color.White, RoundedCornerShape(8.dp))
            .height(70.dp)
            .padding(10.dp),
    ) {
        dataList.forEachIndexed { _, navigationItem ->
            Box(
                modifier = modifier
                    .weight(1f),
                contentAlignment = Alignment.Center
            ) {
                IconText(title = navigationItem.title, icon = navigationItem.icon, context) {
                    "单机了${navigationItem.title}".popTip()
                }
            }
        }
    }

}

@Composable
fun TopAppBar(modifier: Modifier = Modifier) {
    Row(
        modifier = modifier
            .fillMaxWidth()
            .padding(4.dp),
        verticalAlignment = Alignment.CenterVertically,
    ) {
        Column(
            modifier = modifier
                .weight(2f)
                .padding(end = 15.dp)
                .clickable(
                    indication = null,
                    interactionSource = remember { MutableInteractionSource() }
                ) {
                    "单击了日期".popTip()
                },
            Arrangement.Center,
            Alignment.CenterHorizontally
        ) {
            Text(text = "${TimeUtils.formatTime(TimeUtils.YYYY)}年")
            Spacer(modifier.padding(top = 4.dp))
            Row(
                verticalAlignment = Alignment.Bottom,
            ) {
                Text(text = TimeUtils.formatTime(TimeUtils.MM), fontSize = 22.sp)
                Text(text = "月▼")
            }
        }

        Divider(
            color = Color.Gray,
            modifier = modifier
                .width(1.dp)
                .height(30.dp)
        )

        Row(
            modifier = modifier
                .weight(4f)
                .padding(start = 15.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Column(
                modifier = modifier
                    .weight(1f)
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) {
                        "单击了收入".popTip()
                    },
            ) {
                Text(text = "收入")
                Spacer(modifier.padding(top = 4.dp))
                Text(text = "00.00", fontSize = 20.sp)
            }
            Column(
                modifier = modifier
                    .weight(1f)
                    .clickable(
                        indication = null,
                        interactionSource = remember { MutableInteractionSource() }
                    ) {
                        "单击了支出".popTip()
                    },
            ) {
                Text(text = "支出")
                Spacer(modifier.padding(top = 4.dp))
                Text(text = "00.00", fontSize = 20.sp)
            }
        }
    }
}

@Composable
fun TopTipText(context: Context) {
    Box(
        modifier = Modifier
            .padding(4.dp)
            .height(30.dp)
            .fillMaxWidth(),
        contentAlignment = Alignment.CenterStart
    ) {
        Text(
            text = "每日一碗心灵：你努力的模样里藏着父母晚年的幸福！！！",
            fontSize = 18.sp,
            style = TextStyle(
                Brush.verticalGradient(
                    colors = listOf(
                        ColorUtils.getColor(context, R.color.main_color),
                        Color.Black,
                    )
                )
            )
        )
    }
}